const { Service } = require('dx-router-admin');
const { OpendbCityChina } = require('dx-func-entity');

module.exports = class City extends Service {
	constructor(ctx) {
		super(ctx);
		this.entity = OpendbCityChina;
	}
	// 获取城市列表 type 1:省份 2:城市 只能获取两级
	async getCityByType(type) {
		let list = await this.where('type', 'in', type == 2 ? [0, 1] : [0])
			.limit(1000)
			.select();
		return list;
	}
	// 根据父级code获取城市列表
	async getCityByParentCode(parent_code) {
		let list = await this.where('parent_code', parent_code).limit(1000).select();
		return list;
	}
	// 获取所有城市
	async getAllCity() {
		return await this.where('type', 1).limit(1000).order('first_letter', 'asc').select();
	}
	// 根据code获取城市列表
	async getCityByCodes(codes) {
		let params = [];
		if (typeof codes == 'string') {
			if (codes.includes(',')) {
				params = codes.split(',');
			} else {
				params = [codes];
			}
		} else {
			params = codes;
		}
		return await this.where('code', 'in', params).select();
	}
};
